package com.ctim.dao.user;

import com.ctim.model.user.User;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: lb on 2016/8/23.
 * Date:2016-08-23-11:36
 * desc：获取用户信息
 */

@Repository
public class UserDao {
    @Autowired
    @Qualifier("sqlSession")
    private SqlSessionTemplate sqlSession;

    public User selectUserByPhone(String phone) {
        return sqlSession.selectOne("dta.user.selectUserByPhone", phone);
    }

    public User selectUserByUserId(Integer userId) {
        return sqlSession.selectOne("dta.user.selectUserByUserId", userId);
    }

    public List<User> selectUserByPhone(String username, Integer pageOffset, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(username)) {
            map.put("username",username);
        }
        map.put("pageOffset",pageOffset);
        map.put("pageSize",pageSize);
        return sqlSession.selectList("dta.user.selectUserList", map);
    }

    public Integer countUser(String username) {
        Map<String,Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(username)) {
            map.put("username",username);
        }
        return sqlSession.selectOne("dta.user.countUser", map);
    }

    public void insertUser(User user) {
        sqlSession.insert("dta.user.insertUser", user);
    }


    public int deleteUser(Integer userId, Integer updUid) {
        Map<String, Integer> map = new HashMap<>();
        map.put("userId", userId);
        map.put("updUid", updUid);
        return sqlSession.update("dta.user.deleteUser", map);
    }

    public void updateUser(User user) {
        sqlSession.update("dta.user.updateUser", user);
    }

    public Integer countUserNum(Integer userId){
        Map<String,Object> map = new HashMap<>();
        if (userId!=null && userId>0) {
            map.put("userId",userId);
            return sqlSession.selectOne("dta.user.countUserNum", map);
        }
        return 0;
    }

    public List<User> selectUserInfo(Map map){
        return  sqlSession.selectList("dta.user.selectUserInfo",map);
    }

    public void alterUserStatus(Map map){
        sqlSession.update("dta.user.alterUserStatus",map);
    }

    /*修改密码*/
    public int alterpassword(Integer userId,String password){
        Map map=new HashMap();
        map.put("userId",userId);
        map.put("password",password);
        int rows=sqlSession.update("dta.user.alterpassword", map);
        return rows;
    }

    public User selectUserByEmail(String email) {
        return sqlSession.selectOne("dta.user.selectUserByEmail", email);
    }
}
